package com.luofan.controller;


import com.github.pagehelper.PageInfo;
import com.luofan.common.result.CustomException;
import com.luofan.common.result.Result;
import com.luofan.common.result.ResultCodeEnum;
import com.luofan.entity.dto.UpdatePasswordDTO;
import com.luofan.entity.dto.UserQueryDTO;
import com.luofan.entity.po.User;
import com.luofan.entity.vo.LoginVO;
import com.luofan.entity.vo.UserVO;
import com.luofan.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 
 * @since 2025-03-26
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    //登陆
    @PostMapping("/login")
    public Result<LoginVO> login(@RequestBody User user) {
        LoginVO loginVO = null;
        try {
            loginVO = userService.login(user);
            //如果为空说明用户名密码错误
            if (loginVO == null) return Result.build(null, ResultCodeEnum.ACCOUNT_ERROR);
        } catch (CustomException e) {
            //捕获到自定义异常说明账号已停用
            e.printStackTrace();
            return Result.build(null, ResultCodeEnum.ACCOUNT_STOP);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
        return Result.ok(loginVO);
    }

    //管理员 创建用户
    @PostMapping
    public Result<String> createUser(@RequestBody User user) {
        ResultCodeEnum resultCodeEnum = userService.createUser(user);
        return Result.build(null, resultCodeEnum);
    }

    //修改个人信息
    @PutMapping
    public Result<String> updateUser(@RequestBody User user) {
        ResultCodeEnum resultCodeEnum = userService.updateUser(user);
        return Result.build(null, resultCodeEnum);
    }

    //管理员 禁用，解禁用户
    @PutMapping("/forbidden")
    public Result<String> forbiddenUser(@RequestBody User user) {
        ResultCodeEnum resultCodeEnum = userService.forbiddenUser(user);
        return Result.build(null, resultCodeEnum);
    }

    //管理员 重置密码
    @PutMapping("/passwords/reset/{id}")
    public Result<String> resetPasswords(@PathVariable Long id) {
        ResultCodeEnum resultCodeEnum = userService.resetPasswords(id);
        return Result.build(null, resultCodeEnum);
    }

    //修改密码
    @PutMapping("/password")
    public Result<String> updatePassword(@RequestBody UpdatePasswordDTO passwordDTO) {
        ResultCodeEnum resultCodeEnum = userService.updatePassword(passwordDTO);
        return Result.build(null, resultCodeEnum);
    }

    //管理员 更改教师所属教研室
    @PutMapping("/section")
    public Result<String> updateUserSection(@RequestBody User user) {
        ResultCodeEnum resultCodeEnum = userService.updateUserSection(user);
        return Result.build(null, resultCodeEnum);
    }

    //管理员 更改用户权限
    @PutMapping("/role")
    public Result<String> updateUserRole(@RequestBody User user) {
        ResultCodeEnum resultCodeEnum = userService.updateUserRole(user);
        return Result.build(null, resultCodeEnum);
    }

    //管理员 根据条件 分页获取用户信息
    @GetMapping("/condition")
    public Result<PageInfo<UserVO>> listUserByCondition(UserQueryDTO userQueryDTO) {
        PageInfo<UserVO> pageInfo = userService.listUserByCondition(userQueryDTO);
        return Result.ok(pageInfo);
    }


}
